home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_toolbx.arc / VIEWIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-30  |  3.4 KB  |  97 lines

  1. /* viewio.c file - I/O module for VIEW program */
  2. #include "stdio.h"
  3. #include "cminor.h"
  4. #include "viewparm.h"
  5.  
  6. /* fseek mode definitions */
  7. #define CURRENT_REL 1                   /* relative to current position */
  8. #define EOF_REL     2                   /* relative to end of the file  */
  9. #define BOF_REL     0                   /* relative to beginning of file*/
  10.  
  11. #define CTL_Z      26                   /* marks EOF in some text files */
  12.  
  13. FILE *fp ;                              /* file pointer for text file   */
  14. long  filesize ;                        /* size of file in bytes        */
  15. long  int ftell() ;
  16. FILE *gfopen()  ;
  17.  
  18. int open_file(fn)                       /* open a file                  */
  19.   char  fn[]  ;                         /* file name string             */
  20.   {                                     /* return success or failure    */
  21.     fp = gfopen(fn,"r",BIN_MODE) ;
  22.     if(fp != NULL)
  23.       return(SUCCESS) ;
  24.     else  return(FAILURE) ;
  25.   }
  26.  
  27. int set_filesize(chk_cltz)              /* set up filesize              */
  28.   int chk_cltz ;                        /* if = ASC_MODE check CONTROL-Z*/
  29.   {
  30.     long back ;
  31.     int c ;
  32.  
  33.     fseek(fp,0L,EOF_REL) ;              /* get size of file             */
  34.     filesize = ftell(fp) ;
  35.  
  36.     if( chk_cltz != ASC_MODE )          /* controll-z checking needed ? */
  37.       return  ;
  38.  
  39.     /* check last 128 byte block for a CTL_Z */
  40.     back = filesize % 128 ;             /* how for back to start of block*/
  41.     if( (back == 0) && (filesize > 0) ) /* if at start of block         */
  42.       back = 128 ;                      /* last block is full           */
  43.     fseek(fp, filesize - back , BOF_REL) ;
  44.     c = getc(fp) ;
  45.     while( c != EOF )
  46.       { if( c == CTL_Z )                /* look for control-Z           */
  47.           { filesize = ftell(fp) - 1 ;
  48.             return ;               /* found - adjust file size and exit */
  49.           }
  50.         c = getc(fp) ;
  51.       }
  52.   }
  53.  
  54. int move_to(new_pos)                    /* move to specified position   */
  55.   long new_pos ;
  56.   {
  57.     fseek(fp,new_pos,BOF_REL) ;
  58.   }
  59.  
  60. long where_now()                        /* return current position      */
  61.   {
  62.     return( ftell(fp) ) ;
  63.   }
  64.  
  65.  
  66. int get_next_char()                     /* get char at file position    */
  67.   {
  68.     char c ;
  69.  
  70.     if( ftell(fp) == filesize )         /* are we at end of file ?      */
  71.       return( EOF_MARK ) ;              /* yes - return end of file mark*/
  72.     else                                /* no - get a character         */
  73.       { c = getc(fp) ;                  /* get the character            */
  74.         return(toascii(c) ) ;          /* force char to ascii, return it*/
  75.       }
  76.   }
  77.  
  78.  
  79. int get_previous_char()                 /* read the character in front  */
  80.   {                                     /* of the current file position */
  81.     char c ;
  82.  
  83.     if( ftell(fp) != 0L )               /* check for beginning of file  */
  84.       { fseek(fp,-1L,CURRENT_REL) ;     /* back up one character        */
  85.         c = getc(fp) ;                  /* read the character           */
  86.         fseek(fp,-1L,CURRENT_REL) ;     /* back up in front of char read*/
  87.         return( toascii(c) ) ;          /* force it to ASCII and return */
  88.       }
  89.     else return(BOF_MARK) ;             /* it is beginning              */
  90.   }
  91.  
  92.  
  93. int close_file()
  94.   {
  95.     fclose(fp) ;
  96.   }
  97.